<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <!--搜索框-->
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch" placeholder="请输入ip"
                      size="medium"></el-input>
        </div>
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </div>
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
        <!--新增按钮-->
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openAdd"><i class="ti-plus"></i>添加IP</el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                style="width: 100%"
                :default-sort="{prop: 'opat', order: 'descending'}">
            <el-table-column
                    prop="ip"
                    label="IP"
                    header-align="center"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="disabled"
                    label="状态"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <i v-if="!scope.row.disabled" class="fa fa-circle text-success ml5"></i>
                    <i v-if="scope.row.disabled" class="fa fa-circle text-danger ml5"></i>
                </template>
            </el-table-column>
            <el-table-column
                    prop="ctAt"
                    label="创建时间"
                    header-align="center" align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.ctAt)}}
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item v-if="!scope.row.disabled"
                                              :command="{type:'disable', id:scope.row.id, disabled:1}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item v-else-if="scope.row.disabled"
                                              :command="{type:'disable', id:scope.row.id, disabled:0}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'del', id:scope.row.id}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <!--新增/修改按钮弹出的dialog-->
    <el-dialog
            title="新增白名单"
            :visible.sync="addDialogVisible"
            :close-on-click-modal="false"
            width="40%">
        <el-row>
            <el-form :model="formData" ref="addForm" :rules="formRules" size="small" label-width="80px">
                <el-form-item prop="ip" label="IP">
                    <el-input maxlength="100" placeholder="请输入IP"
                              v-model="formData.ip"
                              auto-complete="off"
                              tabindex="1"
                              type="text">
                    </el-input>
                </el-form-item>
            </el-form>
        </el-row>
        <!--        确认/取消 提交修改按钮-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doAdd">确 定</el-button>
        </span>
    </el-dialog>
</div>

<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000);
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                tableData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "opAt",
                    pageOrderBy: "descending",
                    searchKeyword: "",
                    searchDate: "",
                },
                searchDate: [],
                addDialogVisible: false,
                formData: {},
                formRules: {
                    ip: [
                        {required: true, message: '请输入ip', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                let self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/sys/ip/whiteList/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            openAdd: function () {// 新建按钮逻辑
                let self = this;
                self.addDialogVisible = true;
                if (this.$refs["addForm"]) {
                    this.$refs["addForm"].resetFields();
                }
                self.formData = {};
            },
            dropdownCommand: function (command) {//监听下拉框事件
                let self = this;
                //启、禁用
                if('disable' === command.type){
                    let str = "";
                    if(command.disabled === 0){
                        str = '启用';
                    }else {
                        str = '禁用';
                    }
                    self.$confirm('确定要' + str + '该记录', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/sys/ip/whiteList/update", {id: command.id, disabled: command.disabled}, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
                // 删除按钮
                if ("del" === command.type) {
                    self.$confirm('确定要删除该IP', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/sys/ip/whiteList/delete", {id: command.id}, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            },
            doAdd: function () {
                let self = this;
                $.post(base + "/platform/sys/ip/whiteList/add", self.formData, function (data) {
                    if (data.code === 0) {
                        self.$message({
                            message: data.msg,
                            type: 'success'
                        });
                        self.doSearch();
                        self.addDialogVisible = false;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            }
        },
        created: function () {
            this.pageData();
        }
    });
</script>
<!--#
}
#-->